package com.example.amap.gaode.lifecycle

import android.content.Context
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.amap.api.maps.MapView
import com.amap.api.maps.MapsInitializer
import com.example.amap.lib.base.utils.LogUtil
import com.example.amap.gaode.map.GaoDeMapView
import com.example.amap.map.IMapView
import com.example.amap.service.map.IMapService
import java.lang.ref.WeakReference

@Route(path = "/map/gaode/MapServiceImpl")
class MapServiceImpl : IMapService {
    companion object {
        private const val TAG = "LifecycleService"
    }

    private var mapViewWeakReference: WeakReference<MapView?>? = null

    override fun sdkInit(context: Context?) {
        context?.let {
            MapsInitializer.updatePrivacyShow(it, true, true)
            MapsInitializer.updatePrivacyAgree(it, true)
        }
    }

    override fun createMapView(context: Context): IMapView {
        super.createMapView(context)
        mapViewWeakReference = WeakReference(MapView(context))
        LogUtil.d(TAG, "createMapView context: $context")
        return GaoDeMapView(mapViewWeakReference?.get())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mapViewWeakReference?.get()?.onCreate(savedInstanceState)
        LogUtil.d(TAG, "onCreate savedInstanceState: $savedInstanceState")
    }

    override fun onResume() {
        super.onResume()
        mapViewWeakReference?.get()?.onResume()
        LogUtil.d(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        mapViewWeakReference?.get()?.onPause()
        LogUtil.d(TAG, "onPause")
    }

    override fun onDestroy() {
        super.onDestroy()
        mapViewWeakReference?.get()?.onDestroy()
        LogUtil.d(TAG, "onDestroy")
    }
}